package cn.edu.baiyunu.myapplication.ui.screens.add.components

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Backspace
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

/**
 * 自定义金额输入键盘
 */
@Composable
fun CustomKeyboard(
    onNumberClick: (String) -> Unit,
    onDeleteClick: () -> Unit,
    modifier: Modifier = Modifier
) {
    Surface(
        modifier = modifier.fillMaxWidth().height(240.dp),
        color = Color(0x4D000000),
        tonalElevation = 4.dp
    ) {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(16.dp),
            verticalArrangement = Arrangement.spacedBy(10.dp)
        ) {
            // 第一行: 1, 2, 3
            Row(
                modifier = Modifier.fillMaxWidth().weight(1f),
                horizontalArrangement = Arrangement.spacedBy(10.dp)
            ) {
                KeyButton(text = "1", onClick = { onNumberClick("1") }, modifier = Modifier.weight(1f))
                KeyButton(text = "2", onClick = { onNumberClick("2") }, modifier = Modifier.weight(1f))
                KeyButton(text = "3", onClick = { onNumberClick("3") }, modifier = Modifier.weight(1f))
            }
            
            // 第二行: 4, 5, 6
            Row(
                modifier = Modifier.fillMaxWidth().weight(1f),
                horizontalArrangement = Arrangement.spacedBy(10.dp)
            ) {
                KeyButton(text = "4", onClick = { onNumberClick("4") }, modifier = Modifier.weight(1f))
                KeyButton(text = "5", onClick = { onNumberClick("5") }, modifier = Modifier.weight(1f))
                KeyButton(text = "6", onClick = { onNumberClick("6") }, modifier = Modifier.weight(1f))
            }
            
            // 第三行: 7, 8, 9
            Row(
                modifier = Modifier.fillMaxWidth().weight(1f),
                horizontalArrangement = Arrangement.spacedBy(10.dp)
            ) {
                KeyButton(text = "7", onClick = { onNumberClick("7") }, modifier = Modifier.weight(1f))
                KeyButton(text = "8", onClick = { onNumberClick("8") }, modifier = Modifier.weight(1f))
                KeyButton(text = "9", onClick = { onNumberClick("9") }, modifier = Modifier.weight(1f))
            }
            
            // 第四行: ., 0, 删除
            Row(
                modifier = Modifier.fillMaxWidth().weight(1f),
                horizontalArrangement = Arrangement.spacedBy(10.dp)
            ) {
                KeyButton(text = ".", onClick = { onNumberClick(".") }, modifier = Modifier.weight(1f))
                KeyButton(text = "0", onClick = { onNumberClick("0") }, modifier = Modifier.weight(1f))
                Box(
                    modifier = Modifier
                        .weight(1f)
                        .height(50.dp)
                        .clip(RoundedCornerShape(12.dp))
                        .background(color = Color.White.copy(alpha = 0.1f))
                        .clickable(onClick = onDeleteClick),
                    contentAlignment = Alignment.Center
                ) {
                    Icon(
                        imageVector = Icons.Default.Backspace,
                        contentDescription = "删除",
                        tint = Color.White.copy(alpha = 0.9f)
                    )
                }
            }
        }
    }
}

/**
 * 键盘按钮
 */
@Composable
private fun KeyButton(
    text: String,
    onClick: () -> Unit,
    modifier: Modifier = Modifier
) {
    Box(
        modifier = modifier
            .height(50.dp)
            .clip(RoundedCornerShape(12.dp))
            .background(color = Color.White.copy(alpha = 0.1f))
            .clickable(onClick = onClick),
        contentAlignment = Alignment.Center
    ) {
        Text(
            text = text,
            fontSize = 24.sp,
            fontWeight = FontWeight.Medium,
            color = Color.White.copy(alpha = 0.9f),
            textAlign = TextAlign.Center
        )
    }
} 